﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using DragDraw;
using DragDraw.Util;

namespace DragDraw.Tool
{
    public partial class NotePad : UserControl
    {
        public NotePad()
        {
            InitializeComponent();

            m_notes.Add(new Note { Content = "", Title = "" });

            if (WidgetHelper.IsInMySpace == false)
            {
                btnAdd.Visibility = Visibility.Collapsed;
                btnDel.Visibility = Visibility.Collapsed;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            m_proxy = OperationManager.Instance.CreateWidgetServiceProxy(this);
            m_proxy.GetSessionCompleted += OnGetSession;

            m_proxy.GetSession("notes");

            m_saveTimer = new DispatcherTimer();
            m_saveTimer.Interval = new TimeSpan(0, 0, 10);
            m_saveTimer.Tick += new EventHandler(m_saveTimer_Tick);
            m_saveTimer.Start();
        }

        private List<Note> m_notes = new List<Note>();
        private int m_curIndex = 0;
        private WidgetServiceProxy m_proxy;
        private DispatcherTimer m_saveTimer;
        private bool m_dirty;

        void OnGetSession(object sender, DragDraw.UserService.GetSessionCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))// no session at all
                return;
            XmlSerializer xs = new XmlSerializer(typeof(List<Note>));
            XmlReader xr = XmlReader.Create(new StringReader(e.Result));
            m_notes = (List<Note>)xs.Deserialize(xr);
            txtEdit.Text = m_notes[0].Title + m_notes[0].Content;
        }

        void m_saveTimer_Tick(object sender, EventArgs e)
        {
            if (m_dirty)
            {
                StringWriter sw = new StringWriter();
                XmlSerializer xs = new XmlSerializer(typeof(List<Note>));
                xs.Serialize(sw, m_notes);
                m_proxy.SetSession("notes", sw.ToString());
                m_dirty = false;
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            //save current
            m_notes[m_curIndex].Content = txtEdit.Text;

            //new
            m_curIndex = m_notes.Count;
            m_notes.Add(new Note());
            
            //gui
            txtEdit.Text = "";

            m_dirty = true;
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            //edit list
            if (m_notes.Count == 1)//just clear
            {
                m_notes[0].Content = "";
                txtEdit.Text = "";
            }
            else
            {
                m_notes.RemoveAt(m_curIndex);
                m_curIndex = m_curIndex % m_notes.Count;
            }

            m_dirty = true;
        }

        private void btnPre_Click(object sender, RoutedEventArgs e)
        {
            //save current
            m_notes[m_curIndex].Content = txtEdit.Text;
            //change
            int count = m_notes.Count;
            m_curIndex = (m_curIndex + count - 1) % count;
            txtEdit.Text = m_notes[m_curIndex].Content;
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            //save current
            m_notes[m_curIndex].Content = txtEdit.Text;
            //change
            int count = m_notes.Count;
            m_curIndex = (m_curIndex + count + 1) % count;
            txtEdit.Text = m_notes[m_curIndex].Content;
        }

        private void txtEdit_TextChanged(object sender, TextChangedEventArgs e)
        {
            m_dirty = true;
        }
    }

    public class Note
    {
        public string Title = "";
        public string Content = "";
    }
}
